﻿package mortal.game.view.copy.worldBoss
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.worldBoss.WorldBossAllot.*;
    import mortal.game.view.copy.worldBoss.other.*;
    import mortal.game.view.copy.worldBoss.smallBossPanel.*;
    import mortal.game.view.copy.worldBoss.worldBossEnter.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WorldBossController extends CopyControllerBase
    {
        private var _worldBossView:WorldBossHoleView;
        private const preTime:int = 60;
        private const lastTime:int = 300;
        private var _isInHoleBoss:Boolean = false;
        private var _dicBossCodeAllotWin:Dictionary;
        private var lastSelectTime:int;

        public function WorldBossController()
        {
            this._dicBossCodeAllotWin = new Dictionary();
            return;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.WorldBossBelong, this.worldBossBelongHandler);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossHurtList, this.worldBossHurtListAndUpdateWin);
            NetDispatcher.addCmdListener(ServerCommand.SmallBossHurtList, this.onSmallBossHurtList);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossHolePickItem, this.holeBossPickItem);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.checkShowIcon);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossLastHitDrop, this.worldBossLastHitDropHandler);
            Dispatcher.addEventListener(EventName.LeaveScene, this.onLeaveScene);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossBetStart, this.onBetStart);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossBetUpdateInfo, this.onBetUpdateInfo);
            NetDispatcher.addCmdListener(ServerCommand.WorldBossDiceUpdate, this.onDiceUpdate);
            Dispatcher.addEventListener(EventName.WorldBossAllotSelectItem, this.onAllotSelectItem);
            Dispatcher.addEventListener(EventName.WorldBossAllotHide, this.onAllotHide);
            Dispatcher.addEventListener(EventName.Scene_Update, this.ShowHideView);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.checkShowIcon);
            Dispatcher.addEventListener(EventName.WorldBossEnterWindowShow, this.onWorldBossEnterWindowShow);
            Dispatcher.addEventListener(EventName.WorldBossEnterWindowShowBigBoss, this.onWorldBossEnterWindowShowBig);
            return;
        }// end function

        private function checkShowIcon(param1 = null) : void
        {
            if (cache.role.entityInfo.level >= GameConst.WorldBossOpenLevel)
            {
                WorldBossIcon.instance.show();
                ClockManager.instance.addEventListener(TimeEvent.DateChange, this.registDateCall);
                this.registDateCall();
            }
            return;
        }// end function

        private function worldBossLastHitDropHandler(param1:Array = null) : void
        {
            var _loc_2:int = 0;
            if (param1 && param1.length)
            {
                WorldBossLastHitPanel.instance.show();
                _loc_2 = 0;
                while (_loc_2 < param1.length)
                {
                    
                    WorldBossLastHitPanel.instance.addRewardItem(param1[_loc_2].itemData, param1[_loc_2].updateAmount);
                    _loc_2++;
                }
            }
            return;
        }// end function

        private function onWorldBossEnterWindowShow(param1 = null) : void
        {
            if (cache.role.entityInfo.level >= GameConst.WorldBossOpenLevel)
            {
                WorldBossWindowNew.instance.show();
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(21466, GameConst.WorldBossOpenLevel), MsgRollTipsType.msgRollTips3);
            }
            return;
        }// end function

        private function onWorldBossEnterWindowShowBig(param1 = null) : void
        {
            if (cache.role.entityInfo.level >= GameConst.WorldBossOpenLevel)
            {
                WorldBossWindowNew.instance.show();
                WorldBossWindowNew.instance.changeToBigPanel();
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(21466, GameConst.WorldBossOpenLevel, MsgRollTipsType.msgRollTips3));
            }
            return;
        }// end function

        private function registDateCall(param1 = null) : void
        {
            this.registDateCallImpl(9, 0, 0);
            this.registDateCallImpl(12, 0, 0);
            this.registDateCallImpl(15, 0, 0);
            this.registDateCallImpl(18, 0, 0);
            this.registDateCallImpl(21, 0, 0);
            this.registDateCallImpl(24, 0, 0);
            return;
        }// end function

        private function registDateCallImpl(param1:int, param2:int, param3:int) : void
        {
            var _loc_4:* = ClockManager.instance.nowDate.time;
            var _loc_5:* = param1 * 3600 + param2 * 60 + param3;
            var _loc_6:* = _loc_5 - this.preTime;
            var _loc_7:* = _loc_6 % 60;
            var _loc_8:* = Math.floor(_loc_6 % 3600 / 60);
            var _loc_9:* = Math.floor(_loc_6 / 3600);
            var _loc_10:* = new Date(_loc_4);
            _loc_10.hours = _loc_9;
            _loc_10.minutes = _loc_8;
            _loc_10.seconds = _loc_7;
            ClockManager.instance.addDateCall(_loc_10, this.blinkIcon);
            var _loc_11:* = _loc_5 + this.lastTime;
            var _loc_12:* = _loc_11 % 60;
            var _loc_13:* = Math.floor(_loc_11 % 3600 / 60);
            var _loc_14:* = Math.floor(_loc_11 / 3600);
            var _loc_15:* = new Date(_loc_4);
            _loc_15.hours = _loc_14;
            _loc_15.minutes = _loc_13;
            _loc_15.seconds = _loc_12;
            ClockManager.instance.addDateCall(_loc_15, this.unBlinkIcon);
            if (_loc_4 > _loc_10.time && _loc_4 < _loc_15.time)
            {
                this.blinkIcon();
            }
            return;
        }// end function

        private function blinkIcon() : void
        {
            WorldBossIcon.instance.playEffect();
            return;
        }// end function

        private function unBlinkIcon() : void
        {
            WorldBossIcon.instance.stopEffect();
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._worldBossView == null)
            {
                this._worldBossView = new WorldBossHoleView();
            }
            return this._worldBossView as IView;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            if (Cache.instance.copy.isInCopy)
            {
                if (GameMapUtil.curMapState.isHoleBoss)
                {
                    view.show();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrack, false));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
                }
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (!view.isHide)
            {
                view.hide();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrack, true));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
            }
            return;
        }// end function

        private function worldBossBelongHandler(param1:SEntityId) : void
        {
            var _loc_3:EntityInfo = null;
            Cache.instance.copy.worldBoss.belongGuildId = param1;
            var _loc_2:* = cache.entity.getAllEntityInfo();
            for each (_loc_3 in _loc_2)
            {
                
                if (_loc_3.entityInfo.entityId.type == EEntityType._EEntityTypePlayer)
                {
                    _loc_3.updateBossBelong(param1 && param1.id && EntityUtil.equal(_loc_3.entityInfo.guildId, param1) || EntityUtil.equal(_loc_3.entityInfo.entityId, param1));
                }
            }
            _loc_3 = cache.role.roleEntityInfo;
            if (_loc_3.entityInfo.entityId.type == EEntityType._EEntityTypePlayer)
            {
                _loc_3.updateBossBelong(param1 && param1.id && EntityUtil.equal(_loc_3.entityInfo.guildId, param1) || EntityUtil.equal(_loc_3.entityInfo.entityId, param1));
            }
            return;
        }// end function

        private function onLeaveScene(event:DataEvent) : void
        {
            this.worldBossBelongHandler(null);
            return;
        }// end function

        private function worldBossHurtListAndUpdateWin(param1:SWorldBossHurtList) : void
        {
            Cache.instance.copy.worldBoss.worldBossHurtList = param1;
            WorldBossHoleView(this.view).refresh();
            return;
        }// end function

        private function ShowHideView(event:DataEvent = null) : void
        {
            if (GameMapUtil.curMapState.isHoleBoss && !this._isInHoleBoss)
            {
                this._isInHoleBoss = true;
                view.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrack, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
            }
            else if (!GameMapUtil.curMapState.isHoleBoss && this._isInHoleBoss)
            {
                this._isInHoleBoss = false;
                if (isInitView())
                {
                    view.hide();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrack, true));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
                }
            }
            return;
        }// end function

        private function holeBossPickItem(param1:SPlayerItem) : void
        {
            if (Cache.instance.guild.hasGuild)
            {
                GuidePickupItemFly.instance.checkAndFlyToBackpack(param1.itemCode, ModuleType.Guild);
            }
            else
            {
                GuidePickupItemFly.instance.checkAndFlyToBackpack(param1.itemCode);
            }
            return;
        }// end function

        private function onSmallBossHurtList(param1:SFieldBossHurtList) : void
        {
            SmallBossMiniView.instance.show();
            SmallBossMiniView.instance.updateHurtList(param1);
            return;
        }// end function

        private function onBetStart(param1:SGuildItemBetUpdateInfo) : void
        {
            var _loc_2:* = this._dicBossCodeAllotWin[param1.bossCode];
            if (!_loc_2)
            {
                _loc_2 = new WorldBossAllotWindow();
                this._dicBossCodeAllotWin[param1.bossCode] = _loc_2;
            }
            _loc_2.show();
            return;
        }// end function

        private function onBetUpdateInfo(param1:SGuildItemBetUpdateInfo) : void
        {
            var _loc_2:* = this._dicBossCodeAllotWin[param1.bossCode];
            if (_loc_2)
            {
                _loc_2.updateData(param1, cache.copy.worldBoss.getAllotMsg(param1.bossCode));
            }
            return;
        }// end function

        private function onDiceUpdate(param1:SGuildItemDiceUpdateInfo) : void
        {
            var _loc_2:* = this._dicBossCodeAllotWin[param1.bossCode];
            if (_loc_2)
            {
                _loc_2.updateData(cache.copy.worldBoss.getBetInfo(param1.bossCode), cache.copy.worldBoss.getAllotMsg(param1.bossCode));
            }
            return;
        }// end function

        private function onAllotSelectItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = getTimer();
            if (_loc_3 - this.lastSelectTime < 200)
            {
                MsgManager.showRollTipsMsg(Language.getString(21467));
                return;
            }
            this.lastSelectTime = _loc_3;
            var _loc_4:* = _loc_2.itemCode;
            var _loc_5:* = _loc_2.bossCode;
            GameProxy.guild.diceGuildItem(_loc_5, _loc_4);
            return;
        }// end function

        private function onAllotHide(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            cache.copy.worldBoss.clearWorldBoss(_loc_2);
            return;
        }// end function

    }
}
